package edu.gatech.cs2340.teamrocket.model.environment;

import java.io.Serializable;

/**
 * Keeps track of day, season, weather, and time.
 *
 * @author Joe Tacheron
 */

public class Environment implements Serializable {

    public static final int YEAR_LENGTH = 360;

    private static final int SPRING_DATE  = 0;
    private static final int SUMMER_DATE  = YEAR_LENGTH / 4;
    private static final int FALL_DATE    = YEAR_LENGTH / 2;
    private static final int WINTER_DATE  = YEAR_LENGTH * 3 / 4;

    private static final int START_DAY = 0;
    private static final Season START_SEASON = Season.SPRING;
    private static final Weather START_WEATHER = Weather.SUNNYDAY;
    private static final Time START_TIME = Time.DAY;

    private int day;
    private Season season;
    private Weather weather;
    private Time time;

    /**
     * Default constructor that selects a weather.
     */
    public Environment(){
        day     = START_DAY;
        season  = START_SEASON;
        weather = START_WEATHER;
        time    = START_TIME;
    }

    public int getDay(){
        return day;
    }

    public Season getSeason(){
        return season;
    }

    public Weather getWeather(){
        return weather;
    }

    public Time getTime(){
        return time;
    }

    /**
     *
     * @return the maximum speed possible given the current weather.
     */
    public int maxWagonSpeed(){
        return weather.getMaxSpeed();
    }

    /**
     * Increment the day and time, and if appropriate, the season.
     * @param step
     */
    public void incDay(int step){
        day += step;
        day %= YEAR_LENGTH;

        if(day >= SPRING_DATE && day < SUMMER_DATE)
            season = Season.SPRING;
        else if(day >= SUMMER_DATE && day < FALL_DATE)
            season = Season.SUMMER;
        else if(day >= FALL_DATE && day < WINTER_DATE)
            season = Season.FALL;
        else
            season = Season.WINTER;

        switch(time){
            case DAY:
                time = Time.NIGHT;
                break;
            case NIGHT:
                time = Time.DAY;
                break;
        }
    }

    /**
     * Set weather to some random new weather.
     */
    public void changeWeather(){
        weather = season.getRandomWeather();
    }

    /**
     *
     * @return an HTML-formatted String representation of this Environment
     */
    public String toHTML(){
        StringBuilder retval = new StringBuilder();

        retval.append("<html>");
        retval.append("Environment: ");
        retval.append("<font size='2'>");
        retval.append("<p>");
        retval.append("Day: ");
        retval.append(day);
        retval.append("</p>");
        retval.append("<p>");
        retval.append("Season: ");
        retval.append(season);
        retval.append("</p>");
        retval.append("<p>");
        retval.append("Weather: ");
        retval.append(weather);
        retval.append("</p>");
        retval.append("<p>");
        retval.append("Time: ");
        retval.append(time);
        retval.append("</p>");
        retval.append("</font>");
        retval.append("</html>");

        return retval.toString();
    }
}